﻿using sipfe.bean;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace sipfe.util
{
    public class AreasHelper
    {
        private static AreasHelper singleton = null;

        public static AreasHelper getInstance()
        {
            if (singleton == null)
            {
                singleton = new AreasHelper();
            }
            return singleton;
        }

        public string formaTreeJsAreas(List<AutoAyudaBean> lista, string tipoSeleccion,string funcionJs)
        {
            StringBuilder cadena = new StringBuilder();
            String titulo = "Dependencias";

            cadena.Append("<script>\n");
            cadena.Append("d = new dTree('d');\n d.add(0,-1,'").Append(titulo).Append("','','").Append(titulo).Append("','','../../images/trees/base.gif','../../images/trees/base.gif');");
            if (tipoSeleccion.Equals("unitario"))
            {
                cadena.Append(treeJsUnitaria(lista,funcionJs));
            }
            else if (tipoSeleccion.Equals("unitarioLink"))
            {
                cadena.Append(treeJsUnitariaLink(lista, funcionJs));
            }
            else
            {
                cadena.Append(treeJsMultiple(lista, funcionJs));
            }
            cadena.Append("\n document.write(d);\n d.openTo(1,true);\n d.s(0);");
            cadena.Append("\n</script>");

            return cadena.ToString();
        }

        private String treeJsMultiple(List<AutoAyudaBean> lstAutoAyudaBean, string funcionJs) 
   {
	    StringBuilder cadena    =   new StringBuilder();
	    decimal bandera 		=   0;
	    string link 			=   "";
	    string valorLCR		    =   "";

          foreach (AutoAyudaBean area in lstAutoAyudaBean)
          {
              bandera = area.NBanSel;
           if (bandera==1) {
               link = "javascript:" + funcionJs +"('" + area.VDescripcion + "')";
               valorLCR = area.VDescripcion;
               cadena.Append("d.add(").Append(area.NIdDep).Append(",").Append(area.NIdeDepPadre).Append(",'").Append(area.VDescripcion).Append("<input type=checkbox id=\"seleccionado\" name=\"seleccionado[]\"  value=\"").Append(valorLCR).Append("\" size=1 \\>',\"").Append(link).Append("\",'").Append(area.VDescripcion).Append("','','").Append("../../images/trees/folder.gif','../../images/trees/folder_open.gif');\n");

               // link = "javascript:" + funcionJs + "('" + area.VDescripcion + "','" + area.NIdDep + "')";
           
           } else {
               link = "";
               cadena.Append("d.add(").Append(area.NIdDep).Append(",").Append(area.NIdeDepPadre).Append(",'").Append(area.VDescripcion).Append("',\"").Append("\",'").Append(area.VDescripcion).Append("','','").Append("../../images/trees/folder.gif','../../images/trees/folder_open.gif');\n");
           }
       }   
	   return cadena.ToString();
   }
   private String treeJsUnitaria(List<AutoAyudaBean> lstAutoAyudaBean,string funcionJs) 
    {
        StringBuilder cadena    =   new StringBuilder();
        string link             =   "";
        decimal bandera         =   0;

        foreach (AutoAyudaBean area in lstAutoAyudaBean)
        {
            bandera = area.NBanSel;            
            if (bandera==1) {
                link = "javascript:" + funcionJs + "('" + area.VDescripcion + "','" + area.NIdDep + "')";
            } else {
                link = "";
            }
            cadena.Append("d.add(").Append(area.NIdDep).Append(",").Append(area.NIdeDepPadre).Append(",'").Append(area.VDescripcion).Append("',\"").Append(link).Append("\",'").Append(area.VDescripcion).Append("','','").Append("../../images/trees/folder.gif','../../images/trees/folder_open.gif');\n");
        }
        return cadena.ToString();
    }
    private String treeJsUnitariaLink(List<AutoAyudaBean> lstAutoAyudaBean, string funcionJs)
    {
        StringBuilder cadena = new StringBuilder();
        string link = "";
        foreach (AutoAyudaBean area in lstAutoAyudaBean)
        {
            link = "javascript:" + funcionJs + "('" + area.VDescripcion + "','" + area.NIdDep + "')";
            cadena.Append("d.add(").Append(area.NIdDep).Append(",").Append(area.NIdeDepPadre).Append(",'").Append(area.VDescripcion).Append("',\"").Append(link).Append("\",'").Append(area.VDescripcion).Append("','','").Append("../../images/trees/folder.gif','../../images/trees/folder_open.gif');\n");
        }
        return cadena.ToString();
    }

    }
}
